package com.py.outbound.controller;

import java.util.ArrayList;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.py.api.PyCodeApi;
import com.py.outbound.domain.*;
import com.py.outbound.service.*;
import com.py.outbound.domain.DateBeginEnd;
import com.py.outbound.domain.StatisticsDate;
import com.py.outbound.domain.WhOutbound;
import com.py.outbound.service.IWhOutboundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Transient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 出库管理Controller
 * 
 * @author ruoyi
 * @date 2024-12-16
 */
@RestController
@RequestMapping("/outbound")
public class WhOutboundController extends BaseController
{
    @Autowired
    private IWhOutboundService whOutboundService;


    /**
     * 代办事项(待审核)
     */
    @GetMapping("/execute")
    public List<WhOutbound> selectExecute(){
        System.out.println("事项"+whOutboundService.selectExecute());
        return whOutboundService.selectExecute();
    }

    /**
     * 代办事项(待审核)
     */
    @GetMapping("/toExamine")
    public List<WhOutbound> selectToExamine(){
        System.out.println("事项"+whOutboundService.selectToExamine());
        return whOutboundService.selectToExamine();
    }


    /**
     * 概况
     */
    @GetMapping("/overview")
    public List<WhOutbound> selectOverview(@RequestParam Map<String, String> params) {
        String timeStr = params.get("time");
        try {
            Date date = new SimpleDateFormat("yyyy-MM-dd").parse(timeStr);
            System.out.println("概况：" + whOutboundService.selectOverview(date));
            return whOutboundService.selectOverview(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return Collections.emptyList();
        }
    }


    /**
     * 出库排名
     */
    @GetMapping("/ranking")
    public List<WhOutbound> selectWhOutboundRanking(){
        return whOutboundService.selectWhOutboundRanking();
    }


    /**
     * 出库统计
     */
    @RequestMapping("/date")
    public StatisticsDate selectWhOutboundStatistics(DateBeginEnd dateBeginEnd){
        return whOutboundService.selectWhOutboundStatistics(dateBeginEnd);
    }

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private InRejectedService rejectedService;

    @Autowired
    private OdSaleService odSaleService;

    @Autowired
    private RejectedDetailsService rejectedDetailsService;

    @Autowired
    private OdSaleDetailsService odSaleDetailsService;

    @Resource
    private PyCodeApi pyCodeApi;

    @Autowired
    private IPyAttachmentService pyAttachmentService;

    /**
     * 获取编码
     */
    @PostMapping("/getcode")
    public Map<String, Object> getCode(@RequestParam(value = "code",required = false) Integer code){
        System.out.println("进来了"+code);
        return pyCodeApi.generateCode(code);
    }

    /**
     * 查询出库管理列表
     */
    @RequiresPermissions("outbound:outbound:list")
    @GetMapping("/list")
    public TableDataInfo list(WhOutbound whOutbound)
    {
        startPage();
        List<WhOutbound> list = whOutboundService.selectWhOutboundList(whOutbound);
        return getDataTable(list);
    }

    /**
     * 导出出库管理列表
     */
    @RequiresPermissions("outbound:outbound:export")
    @Log(title = "出库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WhOutbound whOutbound)
    {
        List<WhOutbound> list = whOutboundService.selectWhOutboundList(whOutbound);
        ExcelUtil<WhOutbound> util = new ExcelUtil<WhOutbound>(WhOutbound.class);
        util.exportExcel(response, list, "出库管理数据");
    }

    /**
     * 获取出库管理详细信息
     */
    @RequiresPermissions("outbound:outbound:query")
    @GetMapping(value = "/{outboundId}")
    public AjaxResult getInfo(@PathVariable("outboundId") Long outboundId)
    {
        return success(whOutboundService.selectWhOutboundByOutboundId(outboundId));
    }

    /**
     * 新增出库管理
     */
    @Transient
    @RequiresPermissions("outbound:outbound:add")
    @Log(title = "出库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WhOutbound whOutbound)
    {
        List<Object> fileList = whOutbound.getFileList();
        pyCodeApi.saveCode(whOutbound.getCodeId(), whOutbound.getNumber());
        if(fileList.size()>0){
            for (Object file : fileList) {
                if (file instanceof Map) {
                    Map<String, Object> fileMap = (Map<String, Object>) file;
                    Map<String, Object> response = (Map<String, Object>) fileMap.get("response");
                    Object ob=fileMap.get("size");
                    if (response != null) {
                        Map<String, Object> data = (Map<String, Object>) response.get("data");
                        if (data != null) {
                            String name = (String) data.get("name");
                            String url = (String) data.get("url");
                            System.out.println("Name: " + name);
                            System.out.println("URL: " + url);
                            PyAttachment pyAttachment=new PyAttachment();
                            pyAttachment.setBusinessType("出库单");
                            pyAttachment.setOwnershipNumber(whOutbound.getOutboundCode());
                            pyAttachment.setFileName(name);
                            pyAttachment.setSize(Long.parseLong(ob.toString()));
                            pyAttachment.setFilePath(url);
                            pyAttachmentService.insertPyAttachment(pyAttachment);
                        }
                    }
                }
            }
        }
        return toAjax(whOutboundService.insertWhOutbound(whOutbound));
    }

    /**
     * 修改出库管理
     */
    @RequiresPermissions("outbound:outbound:edit")
    @Log(title = "出库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WhOutbound whOutbound)
    {
        pyAttachmentService.deletePyAttachmentByOwnershipNumber(whOutbound.getOutboundCode());

        List<Object> fileList = whOutbound.getFileList();
        pyCodeApi.saveCode(whOutbound.getCodeId(), whOutbound.getNumber());
        if(fileList.size()>0){
            for (Object file : fileList) {
                if (file instanceof Map) {
                    Map<String, Object> fileMap = (Map<String, Object>) file;
                    Map<String, Object> response = (Map<String, Object>) fileMap.get("response");
                    Object ob=fileMap.get("size");
                    if (response != null) {
                        Map<String, Object> data = (Map<String, Object>) response.get("data");
                        if (data != null) {
                            String name = (String) data.get("name");
                            String url = (String) data.get("url");
                            System.out.println("Name: " + name);
                            System.out.println("URL: " + url);
                            PyAttachment pyAttachment=new PyAttachment();
                            pyAttachment.setBusinessType("出库单");
                            pyAttachment.setOwnershipNumber(whOutbound.getOutboundCode());
                            pyAttachment.setFileName(name);
                            pyAttachment.setSize(Long.parseLong(ob.toString()));
                            pyAttachment.setFilePath(url);
                            pyAttachmentService.insertPyAttachment(pyAttachment);
                        }
                    }
                }
            }
        }

        return toAjax(whOutboundService.updateWhOutbound(whOutbound));
    }

    /**
     * 删除出库管理
     */
    @RequiresPermissions("outbound:outbound:remove")
    @Log(title = "出库管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{outboundIds}")
    public AjaxResult remove(@PathVariable Long[] outboundIds)
    {
        return toAjax(whOutboundService.deleteWhOutboundByOutboundIds(outboundIds));
    }

    /**
     * 查询进货部门
     */
    @GetMapping("/selectAllSysDept")
    public List<SysDept> selectAllSysDept() {
        return sysDeptService.selectAllSysDept();
    }

    /**
     * 查询进货部门
     */
    @GetMapping("/selectAllSysDeptByID")
    public List<SysDept> selectAllSysDeptByID(@RequestParam("deptid") Long deptid) {
        return sysDeptService.selectAllSysDeptByParentId(deptid);
    }

    @GetMapping("/selectAllSysUserByParentId")
    public List<SysUser> selectAllSysUserByParentId(@RequestParam("deptid") Long deptid) {
        return sysDeptService.selectAllSysUserByParentId(deptid);
    }

    /**
     * 查询退货管理列表
     */
    @GetMapping("/listRejected")
    public TableDataInfo listRejected(InRejected inRejected)
    {
        startPage();
        List<InRejected> list = rejectedService.selectRejectedList(inRejected);
        return getDataTable(list);
    }

    /**
     * 查询销售出库列表
     */
    @GetMapping("/listOdSale")
    public TableDataInfo listOdSale(OdSale odSale)
    {
        startPage();
        List<OdSale> list = odSaleService.selectOdSaleList(odSale);
        list.forEach(i -> System.out.println("odSale = " + i));
        return getDataTable(list);
    }

    /**
     * 查询进货退货详细
     */
    @GetMapping("/listRejectedDetails")
    public List<RejectedDetails> listRejectedDetails(RejectedDetails rejectedDetails)
    {
        System.out.println(rejectedDetails);
        List<RejectedDetails> list = rejectedDetailsService.selectRejectedDetailsList(rejectedDetails);
        return list;
    }

    /**
     * 查询销售出库详细
     */
    @GetMapping("/listOdSaleDetails")
    public List<OdSaleDetails> listOdSaleDetails(OdSaleDetails odSaleDetails)
    {
        System.out.println(odSaleDetails);
        List<OdSaleDetails> list = odSaleDetailsService.selectOdSaleDetailsList(odSaleDetails);
        return list;
    }

    /**
     * 修改【请填写功能名称】
     */
    @PostMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody WhOutbound whOutbound)
    {
        return toAjax(whOutboundService.outboundStatus(whOutbound));
    }
}
